package com.newsnetwork.user.service.impl;

import com.github.pagehelper.PageHelper;
import com.newsnetwork.api.service.BaseService;
import com.newsnetwork.enums.Sex;
import com.newsnetwork.pojo.AppUser;
import com.newsnetwork.pojo.Fans;
import com.newsnetwork.pojo.vo.RegionRatioVO;
import com.newsnetwork.user.mapper.FansMapper;
import com.newsnetwork.user.service.MyFansService;
import com.newsnetwork.user.service.UserService;
import com.newsnetwork.utils.PagedGridResult;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: HuangDekai
 * @date: 2022/11/15 14:19
 * @version: 1.0
 * @since: jdk8
 * @description: 粉丝管理相关Service实现
 */
@Service
public class MyFansServiceImpl extends BaseService implements MyFansService {
    @Autowired
    private FansMapper fansMapper;

    @Autowired
    private UserService userService;

    @Autowired
    private Sid sid;
    /**
     * 查询当前用户是否关注该作家
     *
     * @param writerId 作家id
     * @param fanId    粉丝id
     * @return true/false
     */
    @Override
    public boolean isMeFollowThisWriter(String writerId, String fanId) {
        Fans fan = new Fans();
        fan.setFanId(fanId);
        fan.setWriterId(writerId);

        int count = fansMapper.selectCount(fan);

        return count > 0;
    }

    /**
     * 粉丝关注该作家
     *
     * @param writerId 作家id
     * @param fanId    粉丝id
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void follow(String writerId, String fanId) {
        AppUser user = userService.getUser(fanId);
        String fanPKId = sid.nextShort();

        Fans fans = new Fans();
        fans.setId(fanPKId);
        fans.setWriterId(writerId);
        fans.setFanId(fanId);

        fans.setFace(user.getFace());
        fans.setFanNickname(user.getNickname());
        fans.setSex(user.getSex());
        fans.setProvince(user.getProvince());

        fansMapper.insert(fans);

        /**
         * 此处采用边路缓存策略, 这里是写策略, 先更新数据库, 再删除缓存
         * 读策略在 news-network-dev-service-user 服务的 UserController.getUserInfo(String userId)中:
         *                先读缓存, 缓存未命中读取数据库中的值, 然后回写给缓存(设置60s过期时间)
         */
        // redis 作家粉丝数累加
        //redisOperator.increment(REDIS_WRITER_FANS_COUNTS + ":" + writerId, 1);
        redisOperator.del(REDIS_WRITER_FANS_COUNTS + ":" + writerId);
        // redis 当前用户的关注数累加
        //redisOperator.increment(REDIS_MY_FOLLOW_COUNTS + ":" + fanId, 1);
        redisOperator.del(REDIS_MY_FOLLOW_COUNTS + ":" + fanId);
    }

    /**
     * 粉丝取消关注该作家
     *
     * @param writerId 作家id
     * @param fanId    粉丝id
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void unfollow(String writerId, String fanId) {
        Fans fans = new Fans();
        fans.setWriterId(writerId);
        fans.setFanId(fanId);

        fansMapper.delete(fans);

        /**
         * 此处采用边路缓存策略, 这里是写策略, 先更新数据库, 再删除缓存
         * 读策略在 news-network-dev-service-user 服务的 UserController.getUserInfo(String userId)中:
         *                先读缓存, 缓存未命中读取数据库中的值, 然后回写给缓存(设置60s过期时间)
         */
        // redis 作家粉丝数累减
        //redisOperator.decrement(REDIS_WRITER_FANS_COUNTS + ":" + writerId, 1);
        redisOperator.del(REDIS_WRITER_FANS_COUNTS + ":" + writerId);
        // redis 当前用户的关注数累减
        //redisOperator.decrement(REDIS_MY_FOLLOW_COUNTS + ":" + fanId, 1);
        redisOperator.del(REDIS_MY_FOLLOW_COUNTS + ":" + fanId);
    }

    /**
     * 分页查询作者的粉丝列表
     *
     * @param writerId 作者id
     * @param page     分页中的第几页
     * @param pageSize 分页每页有几条记录
     * @return 分页好的指定页的查询结果
     */
    @Override
    public PagedGridResult queryMyFansList(String writerId, Integer page, Integer pageSize) {
        Fans fans = new Fans();
        fans.setWriterId(writerId);

        PageHelper.startPage(page, pageSize);
        List<Fans> fansList = fansMapper.select(fans);

        return setterPagedGrid(fansList, page);
    }

    /**
     * 查询作者粉丝数量
     *
     * @param writerId 作家id
     * @return 数量
     */
    @Override
    public Integer queryFansCounts(String writerId) {
        Fans fans = new Fans();
        fans.setWriterId(writerId);

        return fansMapper.selectCount(fans);
    }

    /**
     * 查询作者关注数量
     *
     * @param writerId 作家id
     * @return 关注数量
     */
    @Override
    public Integer queryFollowCounts(String writerId) {
        Fans fans = new Fans();
        fans.setFanId(writerId);

        return fansMapper.selectCount(fans);
    }

    /**
     * 以性别查询作者粉丝数量
     *
     * @param writerId 作家id
     * @param sex      性别
     * @return 数量
     */
    @Override
    public Integer queryFansCounts(String writerId, Sex sex) {
        Fans fans = new Fans();
        fans.setWriterId(writerId);
        fans.setSex(sex.type);

        int count = fansMapper.selectCount(fans);

        return count;
    }

    public static final String[] regions = {"北京", "天津", "上海", "重庆",
            "河北", "山西", "辽宁", "吉林", "黑龙江", "江苏", "浙江", "安徽", "福建", "江西", "山东",
            "河南", "湖北", "湖南", "广东", "海南", "四川", "贵州", "云南", "陕西", "甘肃", "青海", "台湾",
            "内蒙古", "广西", "西藏", "宁夏", "新疆",
            "香港", "澳门"};

    /**
     * 以地域查询作者粉丝数量
     *
     * @param writerId 作家id
     * @return 数量
     */
    @Override
    public List<RegionRatioVO> queryFansCountsByRegion(String writerId) {
        Fans fans = new Fans();
        fans.setWriterId(writerId);

        List<RegionRatioVO> list = new ArrayList<>();
        for (String region : regions) {
            fans.setProvince(region);
            Integer count = fansMapper.selectCount(fans);

            RegionRatioVO regionRatioVO = new RegionRatioVO();
            //对应省份名称
            regionRatioVO.setName(region);
            regionRatioVO.setValue(count);

            list.add(regionRatioVO);
        }

        return list;
    }
}
